package com.cnsuning.jobtool.jobtools.findrowid;

import org.apache.commons.io.FileUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;


public class FindRowByRowId {

//    public static void main(String[] args) throws IOException {
//
//        String srcPathName = "E:\\suning_files\\03\\spark5\\part-00000";
//        String patternRowIdPathName = "E:\\suning_files\\03\\buding\\SO_DATA_SYN_INFO.txt";
//        //提取rowId集合
//        HashSet<String> rowIdSet = new HashSet<>();
//        FileReader rowIdSetReader = new FileReader(patternRowIdPathName);
//        BufferedReader rowIdSetBr = new BufferedReader(rowIdSetReader);
//        String row;
//        while((row = rowIdSetBr.readLine()) != null)
//        {
//            rowIdSet.add(row);
//        }
//        rowIdSetBr.close();
//        rowIdSetReader.close();
//        //获取表名
//        String tableName = patternRowIdPathName.substring(patternRowIdPathName.lastIndexOf("\\")+1, patternRowIdPathName.lastIndexOf("."));
//        String targetPath = "E:\\suning_files\\03\\buding";
//        File targetFloder = new File(targetPath);
//        if (!targetFloder.exists()) targetFloder.mkdirs();
//        String targetFilePath = targetPath +File.separator+ tableName + ".update";
//        File targetFile = new File(targetFilePath);
//        if (targetFile.exists()) targetFile.delete();
//        targetFile.createNewFile();
//        FileReader reader = new FileReader(srcPathName);
//        BufferedReader br = new BufferedReader(reader);
//        String line;
//        Long a = 1L;
//        while ((line = br.readLine()) != null) {
//            //正则筛选
////                update OMSDADM.SO_ORDI_DELI_DETAIL set GLOBAL_ID = '03868125845' where ROW_ID ='868125845';
//            //提取rowid
//            String lineRowId = line.substring(line.indexOf("='") + 2, line.length() - 2);
//            if (rowIdSet.contains(lineRowId) && line.contains(tableName.toUpperCase())) {
//                FileUtils.writeStringToFile(targetFile,line+"\n","utf-8",true);
//            }
//            if (a % 1000000 == 0) {
//                System.out.println("已经:" + a + "条");
//            }
//            a = a + 1;
//
//        }
//        br.close();
//        reader.close();
//        rowIdSet.clear();
//        rowIdSet = null;
//        //out.close();
//    }

    public static void main(String[] args) {
        HashMap<String,String> filenameMap = new HashMap<>();
//        filenameMap.put("SO_DATA_SYN_INFO.txt","E:\\suning_files\\03\\spark5\\part-00000");
        filenameMap.put("SO_ST_ORDI_STATUS.txt","E:\\suning_files\\03\\spark4\\part-00000");
//        filenameMap.put("SO_ORDI_PAY.txt","E:\\suning_files\\03\\spark6\\part-00000aaaai");
//        filenameMap.put("SO_ORDI_APPLY_STATUS_INF.txt","E:\\suning_files\\03\\spark7\\part-00009");
//        filenameMap.put("SO_CHG_STATUS_INF.txt","E:\\suning_files\\03\\spark7\\part-00009");
//        filenameMap.put("SO_ORDI_DEAL_TYPE.txt","E:\\suning_files\\03\\spark8\\part-00000");
//        filenameMap.put("STATUS_SYN_INFO.txt","E:\\suning_files\\03\\spark8\\part-00000");
//        filenameMap.put("SO_SALES_ORDER_MAIN_INF.txt","E:\\suning_files\\03\\spark9\\part-00000");
//        filenameMap.put("SO_ORDI.txt","E:\\suning_files\\03\\spark9\\part-00000");

        for (Map.Entry<String,String> entry : filenameMap.entrySet())
        {
            new Thread(new GenerateUpdate(entry.getValue(),entry.getKey(),"E:\\suning_files\\03\\buding")).start();
        }

    }

    public static class GenerateUpdate implements Runnable{

        private String srcPathName;
        private String rowIdFileName;
        private String targetFloderPath;

        public GenerateUpdate(String srcPathName, String rowIdFileName, String targetFloderPath) {
            this.srcPathName = srcPathName;
            this.rowIdFileName = rowIdFileName;
            this.targetFloderPath = targetFloderPath;
        }

        @Override
        public void run() {
            try {
                String patternRowIdPathName = targetFloderPath+File.separator+rowIdFileName;
                //提取rowId集合
                HashSet<String> rowIdSet = new HashSet<>();
                FileReader rowIdSetReader = new FileReader(patternRowIdPathName);
                BufferedReader rowIdSetBr = new BufferedReader(rowIdSetReader);
                String row;
                while((row = rowIdSetBr.readLine()) != null)
                {
                    rowIdSet.add(row);
                }
                rowIdSetBr.close();
                rowIdSetReader.close();
                //获取表名
                String tableName = patternRowIdPathName.substring(patternRowIdPathName.lastIndexOf("\\")+1, patternRowIdPathName.lastIndexOf("."));
                String targetPath = targetFloderPath;
                File targetFloder = new File(targetPath);
                if (!targetFloder.exists()) targetFloder.mkdirs();
                String targetFilePath = targetPath +File.separator+ tableName + ".update";
                System.out.println("targetFilePath:"+targetFilePath);
                File targetFile = new File(targetFilePath);
                if (targetFile.exists()) targetFile.delete();
                FileReader reader = new FileReader(srcPathName);
                BufferedReader br = new BufferedReader(reader);
                String line;
                Long a = 1L;
                while ((line = br.readLine()) != null) {
                    //正则筛选
    //                update OMSDADM.SO_ORDI_DELI_DETAIL set GLOBAL_ID = '03868125845' where ROW_ID ='868125845';
                    //提取rowid
                    String lineRowId = line.substring(line.indexOf("='") + 2, line.length() - 2);
//                    if (rowIdSet.contains(lineRowId) && line.contains(tableName.toUpperCase())) {
                    if (rowIdSet.contains(lineRowId)) {
                        FileUtils.writeStringToFile(targetFile,line+"\n","utf-8",true);
                    }
                    if (a % 1000000 == 0) {
                        System.out.println("已经:" + a + "条");
                    }
                    a = a + 1;

                }
                br.close();
                reader.close();
                rowIdSet.clear();
                rowIdSet = null;
                //out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
